|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout
The ProportionLayout
class is a flexible layout manager that aligns components
vertically and horizontally, without requiring that the components be of the same size.
ProportionLayout
class defines a grid wherein components can be laid out. Each
component managed by a ProportionLayout
is associated with an instance of ProportionConstraints
. The ProportionConstraints
object specifies where a
component's display area should be located on the grid and how the component should be positioned
within its display area. In addition to its constraints object, the ProportionLayout
also considers each component's minimum and preferred sizes in order to determine a component's
size.
The following figures show six components (all buttons) managed by a proportion layout. Figure 1
shows the layout for the components with their preferred size and Figure 2 shows the layout for
the same components resized to a larger size.
![]() |
Figure 1: the example with its preferred size |
![]() |
Figure 2: the example resized to a larger size |
import java.awt.*; import javax.swing.*; import net.sourceforge.simplegamenet.util.proportionlayout.*; public class LayoutTester extends JFrame { public static void main(String[] args) { LayoutTester currentFrame = new LayoutTester(); currentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); currentFrame.pack(); currentFrame.show(); } public LayoutTester() { super("LayoutTester"); Container contentPanel = getContentPane(); ProportionLayout layout = new ProportionLayout(); // First add all the columns to the ProportionLayout layout.appendColumn(10); // column 0 // Column 0 will be an empty space of width 10 layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // column 1 // Column 1 will always be the greatest preferred width of all it's components // Column 1 will never get any of the additional width layout.appendColumn(10); // column 2 // Column 2 will be an empty space of width 10 layout.appendColumn(0, 1.0); // column 3 // Column 3 will always be the same width as column 5 // Column 3 and 5 will devide any of the additional width between them layout.appendColumn(10); // column 4 // Column 4 will be an empty space of width 10 layout.appendColumn(0, 1.0); // column 5 // Column 5 will always be the same width as column 3 // Column 3 and 5 will devide any of the additional width between them layout.appendColumn(10); // column 6 // Column 6 will be an empty space of width 10 // Then add all the rows to the ProportionLayout layout.appendRow(10); // row 0 // Row 0 will be an empty space of width 10 layout.appendRow(0, ProportionLayout.NO_PROPORTION); // row 1 // Row 1 will always be the greatest preferred height of all it's components // Row 1 will never get any of the additional height layout.appendRow(10); // row 2 // Row 2 will be an empty space of width 10 layout.appendRow(0, ProportionLayout.NO_PROPORTION); // row 3 // Row 3 will always be the greatest preferred height of all it's components // Row 3 will never get any of the additional height layout.appendRow(10); // row 4 // Row 4 will be an empty space of width 10 layout.appendRow(0, 1.0); // row 5 // Row 5 will get all of the additional height layout.appendRow(10); // row 6 // Row 6 will be an empty space of width 10 contentPanel.setLayout(layout); contentPanel.add(new JButton("B1"), new ProportionConstraints(1, 1) ); // column 1, row 1 contentPanel.add(new JButton("Button 2 takes up a lot of space"), new ProportionConstraints(3, 1) ); // column 3, row 1 contentPanel.add(new JButton("Button 3"), new ProportionConstraints(5, 1) ); // column 5, row 1 contentPanel.add(new JButton("B4"), new ProportionConstraints(1, 3, 3, 1) ); // column 1, row 3 with width 3, height 1 contentPanel.add(new JButton("Button 5"), new ProportionConstraints(1, 5) ); // column 1, row 5 contentPanel.add(new JButton("B6"), new ProportionConstraints(5, 3, 1, 3) ); // column 5, row 3 with width 1, height 3 // ProportionConstaints has additional constructors with more parameters // that allow a more flexible control of how the component will behave // inside it's own cell. } }
Field Summary | |
protected TreeSet |
columnLineParts
Contains all the columnLineParts |
protected ArrayList |
columnLines
Contains all the columnLines for this ProportionLayout. |
protected double |
columnProportionTotal
Contains the total amount of columnLines for this ProportionLayout. |
protected HashMap |
linePartCouples
Contains which rowLine and columnLine belong together. |
static double |
NO_PROPORTION
Make the column or row have it's preferred width or height, without stretching. |
protected TreeSet |
rowLineParts
Contains all the rowLineParts |
protected ArrayList |
rowLines
Contains all the rowLines for this ProportionLayout. |
protected double |
rowProportionTotal
Contains the total amount of rowLines for this ProportionLayout. |
Constructor Summary | |
ProportionLayout()
Creates a new instance of ProportionLayout |
Method Summary | |
void |
addLayoutComponent(Component component,
Object object)
Adds a component to the layout, using the specified ProportionConstraints of
that object. |
void |
addLayoutComponent(String name,
Component component)
This method isn't supported. |
int |
appendColumn()
Creates a new column at the end of the grid for this ProportionLayout. |
int |
appendColumn(int minimumWidth)
Creates a new column at the end of the grid for this ProportionLayout. |
int |
appendColumn(int minimumWidth,
double lineProportion)
Creates a new column at the end of the grid for this ProportionLayout. |
int |
appendRow()
Creates a new row at the end of the grid for this ProportionLayout. |
int |
appendRow(int minimumHeight)
Creates a new row at the end of the grid for this ProportionLayout. |
int |
appendRow(int minimumHeight,
double lineProportion)
Creates a new row at the end of the grid for this ProportionLayout. |
protected Dimension |
arrangeGrid(Container parent,
int sizeType)
Arranges the grid for the given container of components and specifies the sizeType of it. |
float |
getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis. |
float |
getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis. |
void |
invalidateLayout(Container parent)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. |
void |
layoutContainer(Container parent)
Lays out the specified container using this proportion layout. |
Dimension |
maximumLayoutSize(Container parent)
Returns the maximum dimensions for this layout given the components in the specified target container. |
Dimension |
minimumLayoutSize(Container parent)
Determines the minimum size of the target container using this proportion layout. |
Dimension |
preferredLayoutSize(Container parent)
Determines the preferred size of the target container using this proportion layout. |
void |
removeLayoutComponent(Component component)
Removes a component from the ProportionLayout. |
String |
toString()
Returns a string representation of this proportion layout's values. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final double NO_PROPORTION
protected ArrayList columnLines
protected ArrayList rowLines
protected double columnProportionTotal
protected double rowProportionTotal
protected HashMap linePartCouples
protected TreeSet columnLineParts
protected TreeSet rowLineParts
Constructor Detail |
public ProportionLayout()
Method Detail |
public int appendColumn() throws IllegalArgumentException
IllegalArgumentException
- if one of the arguments is an illegal argument.public int appendColumn(int minimumWidth) throws IllegalArgumentException
minimumWidth
- Spicifies the minimum width for this column.
IllegalArgumentException
- if one of the arguments is an illegal argument.public int appendColumn(int minimumWidth, double lineProportion) throws IllegalArgumentException
minimumWidth
- specifies the minimum width for this column.lineProportion
- specifies the proportion for this column.
IllegalArgumentException
- if one of the arguments is an illegal argument.public int appendRow() throws IllegalArgumentException
IllegalArgumentException
- if one of the arguments is an illegal argument.public int appendRow(int minimumHeight) throws IllegalArgumentException
minimumHeight
- specifies the minimum height for this row.
IllegalArgumentException
- if one of the arguments is an illegal argument.public int appendRow(int minimumHeight, double lineProportion) throws IllegalArgumentException
minimumHeight
- specifies the minimum height for this row.lineProportion
- specifies the proportion for this row.
IllegalArgumentException
- if one of the arguments is an illegal argument.protected Dimension arrangeGrid(Container parent, int sizeType)
parent
- specified the container to be laid out.sizeType
- specifies the sizeType for this container.
public void addLayoutComponent(Component component, Object object) throws IllegalArgumentException, IndexOutOfBoundsException
ProportionConstraints
of
that object.
addLayoutComponent
in interface LayoutManager2
component
- specifies the component to lay out.object
- specifies the cell in which the component is to be laid out.
IllegalArgumentException
- if the object used as parameter is not of the
ProportionConstraints type.
IndexOutOfBoundsException
- if the index of the object in which you are putting the
component doesn't exist.public void addLayoutComponent(String name, Component component)
addLayoutComponent
in interface LayoutManager
name
- the name of the component.component
- the component to be added.public void removeLayoutComponent(Component component)
removeLayoutComponent
in interface LayoutManager
component
- specifies which component has to be removed.public Dimension minimumLayoutSize(Container parent)
minimumLayoutSize
in interface LayoutManager
parent
- the component to be laid out
public Dimension preferredLayoutSize(Container parent)
preferredLayoutSize
in interface LayoutManager
parent
- the container to be laid out.
public Dimension maximumLayoutSize(Container parent)
maximumLayoutSize
in interface LayoutManager2
parent
- the component which needs to be laid out.
public float getLayoutAlignmentX(Container parent)
getLayoutAlignmentX
in interface LayoutManager2
parent
- the component/container which needs to be aligned along the x axis.
public float getLayoutAlignmentY(Container parent)
getLayoutAlignmentY
in interface LayoutManager2
parent
- the component/container which needs to be aligned along the y axis.
public void invalidateLayout(Container parent)
invalidateLayout
in interface LayoutManager2
parent
- the container that needs to be invalidated.public void layoutContainer(Container parent)
ProportionLayout
object.
layoutContainer
in interface LayoutManager
parent
- the container in which to do the layout.public String toString()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |